package com.util.format;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by kuibu on 16/1/22.
 */
public class TimeFormat {

    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp 需要和当前时间比较的时间戳
     * @return
     */
    public static String convertTimeToFormat(long timeStamp) {
        long curTime = System.currentTimeMillis() / (long) 1000;
        long time = curTime - timeStamp;

        if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600) {
            return time / 60 + "分钟前";
        } else if (time >= 3600 && time < 3600 * 24) {
            return time / 3600 + "小时前";
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 2) {
            return "昨天";
        } else if (time >= 3600 * 24 * 2 && time < 3600 * 24 * 7) {
            return time / (3600 * 24) + "天前";
        } else if (time >= 3600 * 24 * 7 && time < 3600 * 24 * 14) {
            return "1周前";
        } else if (time >= 3600 * 24 * 14 && time < 3600 * 24 * 21) {
            return "2周前";
        } else if (time >= 3600 * 24 * 21 && time < 3600 * 24 * 28) {
            return "3周前";
        } else if (time >= 3600 * 24 * 28 && time < 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 + "个月前";
        } else if (time >= 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 / 12 + "年前";
        } else {
            return "刚刚";
        }
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param date 需要和当前时间比较的日期
     * @return
     */
    public static String convertTimeToFormat(Date date) {
        long curTime = System.currentTimeMillis() / (long) 1000;
        long time = curTime - date.getTime() / (long) 1000;

        if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600 * 24) {
            return "今天";
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 2) {
            return "昨天";
        } else if (time >= 3600 * 24 * 2 && time < 3600 * 24 * 7) {
            return time / (3600 * 24) + "天前";
        } else if (time >= 3600 * 24 * 7 && time < 3600 * 24 * 14) {
            return "1周前";
        } else if (time >= 3600 * 24 * 14 && time < 3600 * 24 * 21) {
            return "2周前";
        } else if (time >= 3600 * 24 * 21 && time < 3600 * 24 * 28) {
            return "3周前";
        } else if (time >= 3600 * 24 * 28 && time < 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 + "个月前";
        } else if (time >= 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 / 12 + "年前";
        } else {
            return "刚刚";
        }
    }

    /**
     * 将一个时间戳转换成16-04-20样式字符串
     *
     * @param timeStamp 参数
     * @return
     */
    public static String yearMonthDay(long timeStamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
        return simpleDateFormat.format(new Date(timeStamp));
    }

    /**
     * 将一个时间戳转换成04-20 15:23样式字符串
     *
     * @param timeStamp 参数
     * @return
     */
    public static String monthDayHourMin(long timeStamp) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd hh:mm");
        return simpleDateFormat.format(new Date(timeStamp));
    }
}
